<template>
  <div id="info" class="mytable">
    <div v-if="loadingText" style="padding: 10px;">{{ loadingText }}</div>
    <el-scrollbar v-else style="height: calc(100vh - 184px)">
      <template v-if="unitInfo && (sourceTable==='depart' || sourceTable==='centre_sy')">
        <template v-if="infoTableName === 'first'">
          <el-form :model="unitInfo" ref="unitInfoForm1" inline-message>
            <table cellspacing="0" cellpadding="0" class="bztable">
              <tr>
                <th>机构全称</th>
                <td>{{ unitInfo.dname }}</td>
                <th>其他全称</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="dname2" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="unitInfo.dname2" maxlength="500"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.dname2 }}</td>
              </tr>
              <tr>
                <th>机构规格</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="dgrade" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.dgrade">
                      <el-option v-for="item in dgradeOptions" :key="'dgrade_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.dgradeText }}</td>
                <th>批准设立文号</th>
                <td v-if="flag === 'user'" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                  <el-form-item prop="ddocNo">
                    <el-input v-model="unitInfo.ddocNo" maxlength="150"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.ddocNo }}</td>
              </tr>
              <tr>
                <th>机构类别</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-form-item prop="dclass" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.dclass">
                      <el-option v-for="item in dclassOptions" :key="'dclass_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else colspan="3">{{ unitInfo.dclassText }}</td>
              </tr>
            </table>
          </el-form>
        </template>

        <template v-else-if="infoTableName === 'second'">
          <el-form :model="unitInfo" ref="unitInfoForm2" inline-message>
            <table cellspacing="0" cellpadding="0" class="bztable">
              <tr>
                <th>机构全称</th>
                <td>{{ unitInfo.dname }}</td>
                <th>其他全称</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="dname2" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="unitInfo.dname2" maxlength="500"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.dname2 }}</td>
              </tr>
              <tr>
                <th>机构规格</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="dgrade" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.dgrade">
                      <el-option v-for="item in dgradeOptions" :key="'dgrade_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.dgradeText }}</td>
                <th>批准设立文号</th>
                <td v-if="flag === 'user'" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                  <el-form-item prop="ddocNo">
                    <el-input v-model="unitInfo.ddocNo" maxlength="150"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.ddocNo }}</td>
              </tr>
              <tr>
                <th>统一社会信用代码</th>
                <td v-if="flag === 'user'">
                  <el-input v-model="unifyCode" maxlength="18"></el-input>
                </td>
                <td v-else>{{ unifyCode }}</td>
                <th>编制使用层次</th>
                <td v-if="flag === 'user'">
                  <el-select v-model="bzsycj">
                    <el-option v-for="item in bzsycjOptions" :key="'bzsycj_' + item.value" :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </td>
                <td v-else>{{ bzsycjText }}</td>
              </tr>
            </table>
          </el-form>
          <el-form :model="lxfs" ref="lxfsForm1" inline-message>
            <table cellspacing="0" cellpadding="0" class="bztable">
              <tr>
                <th>通讯地址</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsTxdz" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="lxfs.lxfsTxdz" maxlength="200"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsTxdz }}</td>
                <th>邮政编码</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsYzbm" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model.trim="lxfs.lxfsYzbm"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsYzbm }}</td>
              </tr>
              <tr>
                <th>联系人</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsLxr" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="lxfs.lxfsLxr" maxlength="20"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsLxr }}</td>
                <th>固定电话</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsLxdh" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="lxfs.lxfsLxdh" maxlength="20"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsLxdh }}</td>
              </tr>
              <tr>
                <th>中文域名1</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsZwym1" maxlength="200"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsZwym1 }}</td>
              </tr>
              <tr>
                <th>中文域名2</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsZwym2" maxlength="200"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsZwym2 }}</td>
              </tr>
              <tr>
                <th>英文域名1</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsYwym1" maxlength="100"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsYwym1 }}</td>
              </tr>
              <tr>
                <th>英文域名2</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsYwym2" maxlength="100"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsYwym2 }}</td>
              </tr>
              <tr>
                <th>备注</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsBz"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsBz}}</td>
              </tr>
            </table>
          </el-form>
        </template>

        <template v-else-if="infoTableName === 'third'">
          <el-form :model="unitInfo" ref="unitInfoForm3" inline-message>
            <table cellspacing="0" cellpadding="0" class="bztable">
              <tr>
                <th>单位全称</th>
                <td>{{ unitInfo.mc }}</td>
                <th>机构属性</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="jgsx" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.jgsx">
                      <el-option v-for="item in jgsxOptions" :key="'jgsx_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.jgsxText }}</td>
              </tr>
              <tr>
                <th>其他全称</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="demc" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="unitInfo.demc" maxlength="500"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.demc }}</td>
                <th>规范简称</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="mcGfjc" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="unitInfo.mcGfjc" maxlength="500"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.mcGfjc }}</td>
              </tr>
              <tr>
                <th>机构规格</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="jggg" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.jggg">
                      <el-option v-for="item in jgggOptions" :key="'jggg_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.jgggText }}</td>
                <th>主管部门（举办单位）</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="zgbm" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="unitInfo.zgbm" maxlength="500"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.zgbm }}</td>
              </tr>
              <tr>
                <th>事业单位类别</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="sydwlx" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.sydwlx">
                      <el-option v-for="item in sydwlxOptions" :key="'sydwlx_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.sydwlxText }}</td>
                <th>经费形式</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="jfxs" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.jfxs">
                      <el-option v-for="item in jfxsOptions" :key="'jfxs_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.jfxText }}</td>
              </tr>
              <tr>
                <th style="vertical-align:middle">京内京外</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="jnjw" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.jnjw">
                      <el-option v-for="item in jnjwOptions" :key="'jnjw_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else style="vertical-align:middle">{{ unitInfo.jnjw == 0 ? '京内' : '京外' }}</td>
                <th style="vertical-align:middle">是否人员参照公务员法管理</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="ifCg" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="unitInfo.ifCg">
                      <el-option v-for="item in ifCgOptions" :key="'ifCg_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else style="vertical-align:middle">{{ unitInfo.ifCg ? '是' : '否' }}</td>
              </tr>
              <tr>
                <th>单位设立时间</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="clsj" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-date-picker v-model="unitInfo.clsj" type="date" value-format="yyyy-MM-dd"/>
                  </el-form-item>
                </td>
                <td v-else>{{ fclsj(unitInfo.clsj) }}</td>
                <th>批准设立文号</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="jgbzwh" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="unitInfo.jgbzwh" maxlength="150"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.jgbzwh }}</td>
              </tr>
              <tr>
                <th>法定代表人</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="incorporator" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="unitInfo.incorporator" maxlength="20"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ unitInfo.incorporator }}</td>
                <th>统一社会信用代码</th>
                <td v-if="flag === 'user'">
                  <el-input v-model="unifyCode" maxlength="18"></el-input>
                </td>
                <td v-else>{{ unifyCode }}</td>
              </tr>
              <tr>
                <th>编制使用层次</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-select v-model="bzsycj">
                    <el-option v-for="item in bzsycjOptions" :key="'bzsycj_' + item.value" :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </td>
                <td v-else colspan="3">{{ bzsycjText }}</td>
                <!-- <th>系统类别</th>
                <td>国务院</td> -->
              </tr>
            </table>
          </el-form>
          <el-form :model="lxfs" ref="lxfsForm2" inline-message>
            <table cellspacing="0" cellpadding="0" class="bztable">
              <tr>
                <th>通讯地址</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsTxdz" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="lxfs.lxfsTxdz" maxlength="200"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsTxdz }}</td>
                <th>邮政编码</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsYzbm" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model.trim="lxfs.lxfsYzbm"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsYzbm }}</td>
              </tr>
              <tr>
                <th>联系人</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsLxr" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="lxfs.lxfsLxr" maxlength="20"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsLxr }}</td>
                <th>固定电话</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="lxfsLxdh" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="lxfs.lxfsLxdh" maxlength="20"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ lxfs.lxfsLxdh }}</td>
              </tr>
              <tr>
                <th>中文域名1</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsZwym1" maxlength="200"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsZwym1 }}</td>
              </tr>
              <tr>
                <th>中文域名2</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsZwym2" maxlength="200"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsZwym2 }}</td>
              </tr>
              <tr>
                <th>英文域名1</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsYwym1" maxlength="100"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsYwym1 }}</td>
              </tr>
              <tr>
                <th>英文域名2</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsYwym2" maxlength="100"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsYwym2 }}</td>
              </tr>
              <tr>
                <th>备注</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-input v-model="lxfs.lxfsBz"></el-input>
                </td>
                <td v-else colspan="3">{{ lxfs.lxfsBz}}</td>
              </tr>
            </table>
          </el-form>
        </template>
      </template>
      <template v-else>
        <template v-if="sourceTable === 'office'">
          <el-form :model="officeInfo" ref="officeInfoForm" inline-message>
            <table cellspacing="0" cellpadding="0" class="bztable">
              <tr>
                <th>机构全称</th>
                <td>{{ officeInfo.oname }}</td>
                <th>其他全称</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="oname2" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="officeInfo.oname2" maxlength="500"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ officeInfo.oname2 }}</td>
              </tr>
              <tr>
                <th>机构规格</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="ograde" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="officeInfo.ograde">
                      <el-option v-for="item in dgradeOptions" :key="'ograde_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ officeInfo.ogradeText }}</td>
                <th>批准文号</th>
                <td v-if="flag === 'user'" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                  <el-form-item prop="odocNo">
                    <el-input v-model="officeInfo.odocNo" maxlength="150"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ officeInfo.odocNo }}</td>
              </tr>
              <tr>
                <th>机构类别</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-form-item prop="oclass" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="officeInfo.oclass">
                      <el-option v-for="item in oclassOptions" :key="'oclass_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else colspan="3">{{ officeInfo.oclassText }}</td>
              </tr>
            </table>
          </el-form>
        </template>

        <template v-if="sourceTable === 'centre_sy_nsjg'">
          <el-form :model="centreSyNsjgInfo" ref="centreSyNsjgInfoForm" inline-message>
            <table cellspacing="0" cellpadding="0" class="bztable">
              <tr>
                <th>机构全称</th>
                <td>{{ centreSyNsjgInfo.slnName }}</td>
                <th>其他全称</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="slnName2" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-input v-model="centreSyNsjgInfo.slnName2" maxlength="500"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ centreSyNsjgInfo.slnName2 }}</td>
              </tr>
              <tr>
                <th>机构规格</th>
                <td v-if="flag === 'user'">
                  <el-form-item prop="slnJggg" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="centreSyNsjgInfo.slnJggg">
                      <el-option v-for="item in jgggOptions" :key="'slnJggg_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else>{{ centreSyNsjgInfo.slnJgggText }}</td>
                <th>批准文号</th>
                <td v-if="flag === 'user'" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                  <el-form-item prop="slnJgbzwh">
                    <el-input v-model="centreSyNsjgInfo.slnJgbzwh" maxlength="150"></el-input>
                  </el-form-item>
                </td>
                <td v-else>{{ centreSyNsjgInfo.slnJgbzwh }}</td>
              </tr>
              <tr>
                <th>机构类别</th>
                <td v-if="flag === 'user'" colspan="3">
                  <el-form-item prop="slnClass" :rules="[{ required: true, message: '必填项', trigger: 'blur' }]">
                    <el-select v-model="centreSyNsjgInfo.slnClass">
                      <el-option v-for="item in slnclassOptions" :key="'slnClass_' + item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </td>
                <td v-else colspan="3">{{ centreSyNsjgInfo.slnClassText }}</td>
              </tr>
            </table>
          </el-form>
        </template>
      </template>
      <div v-if="flag === 'user'" style="display: flex; justify-content: center; margin-bottom: 20px;">
        <el-button type="primary" :loading="btnLoading" @click="saveData()">保存</el-button>
      </div>
    </el-scrollbar>
  </div>
</template>

<script>
export default {
  name: 'DetailInfo',
  props: {
    jgsyCode: '',
    flag: '',
    infoTableName: '',
    sourceTable: '',
    officeDetailInfo: {
      required: false,
      type: Object,
      default: () => ({})
    },
    centreSyNsjgDetailInfo: {
      required: false,
      type: Object,
      default: () => ({})
    }
  },
  watch: {
    jgsyCode: {
      handler () {
        this.loadingText = '正在加载中，请稍后...'
        this.getDict()
        if (this.sourceTable !== 'office' && this.sourceTable !== 'centre_sy_nsjg') {
          this.getDetail(this.sourceTable)
        }
      },
      immediate: true
    },
    officeDetailInfo: {
      handler () {
        this.loadingText = '正在加载中，请稍后...'
        if (this.sourceTable === 'office') {
          this.officeInfo = this.officeDetailInfo
          this.loadingText = ''
        }
      }
    },
    centreSyNsjgDetailInfo: {
      handler () {
        this.loadingText = '正在加载中，请稍后...'
        if (this.sourceTable === 'centre_sy_nsjg') {
          this.centreSyNsjgInfo = this.centreSyNsjgDetailInfo
          this.loadingText = ''
        }
      }
    }
  },
  data () {
    return {
      dictList: [],
      loadingText: '正在加载中，请稍后...',
      unitInfo: {},
      bd: [],
      dwbd: [],
      lxfs: {},
      unifyCode: '',
      bzsycj: '',
      allUnit: {},
      dgradeOptions: [], // 机构规格
      dclassOptions: [], // 机构类别
      jgsxOptions: [], // 机构属性
      jgggOptions: [], // 机构规格
      sydwlxOptions: [], // 事业单位类别
      jfxsOptions: [], // 经费形式
      bzsycjOptions: [], // 编制使用层次
      oclassOptions: [], // 机构类别
      slnclassOptions: [], // 机构类别
      jnjwOptions: [
        {label: '京内', value: '0'},
        {label: '京外', value: '1'}
      ],
      ifCgOptions: [
        {label: '是', value: 1},
        {label: '否', value: 0}
      ],
      btnLoading: false,
      officeInfo: {},
      centreSyNsjgInfo: {}
    }
  },
  methods: {
    // 获取字典
    getDict () {
      const para = {
        pageNum: 1,
        pageSize: 1000
      }
      this.$http
        .post(this.$http.defaults.headcountAPI + `api/busDict/queryByPage`, para)
        .then(response => {
          if (response.data.success) {
            this.dictList = response.data.result.records
            this.dgradeOptions = this.dictList.filter(item => item.businessField === 'D_GRADE')
            this.dclassOptions = this.dictList.filter(item => item.businessField === 'D_CLASS')
            this.jgsxOptions = this.dictList.filter(item => item.businessField === 'JGSX')
            this.jgggOptions = this.dictList.filter(item => item.businessField === 'JGGG')
            this.sydwlxOptions = this.dictList.filter(item => item.businessField === 'SYDWLX')
            this.jfxsOptions = this.dictList.filter(item => item.businessField === 'JFXS')
            this.bzsycjOptions = this.dictList.filter(item => item.businessField === 'jgsy_bzsycj')
            this.oclassOptions = this.dictList.filter(item => item.businessField === 'O_CLASS')
            this.slnclassOptions = this.dictList.filter(item => item.businessField === 'SLN_CLASS')
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    // 获取基本信息
    getDetail (sourceTable) {
      this.unitInfo = {}
      this.bd = []
      this.dwbd = []
      this.lxfs = {}
      this.unifyCode = ''
      this.allUnit = {}
      this.$http
        .get(this.$http.defaults.headcountAPI + `api/xml/getUnitDetails?jgsyCode=` + this.jgsyCode + `&sourceTable=` + sourceTable)
        .then(response => {
          if (response.data.success) {
            let alldata = response.data.result
            this.unifyCode = response.data.result.unifyCode
            this.bzsycj = response.data.result.bzsycj
            this.bzsycjText = response.data.result.bzsycjText
            if (response.data.result.allUnit) {
              this.allUnit = response.data.result.allUnit
            }
            if (sourceTable === 'centre') {
              this.unitInfo = (alldata.centre && alldata.centre.length > 0) ? alldata.centre[0] : {}
              this.bd = alldata.centre_bd
              this.bd.forEach((item, index) => {
                if (item.bmldzsBd) {
                  this.bd[index].bmldbz = item.bmldzsBd.bmldzzbz + item.bmldzsBd.bmldfzbz
                  this.bd[index].nsldbz = item.bmldzsBd.nsldzzbz + item.bmldzsBd.nsldfzbz
                  this.bd[index].bmqtZzbzr = item.bmldzsBd.bmqtZzbzr
                  this.bd[index].bmqtZs = item.bmldzsBd.bmqtZs
                  this.bd[index].bmqtZy = item.bmldzsBd.bmqtZy
                  this.bd[index].bmqtJwsj = item.bmldzsBd.bmqtJwsj
                  this.bd[index].bmqtQt = item.bmldzsBd.bmqtQt
                  this.bd[index].nsqtZzbzr = item.bmldzsBd.nsqtZzbzr
                  this.bd[index].nsqtZs = item.bmldzsBd.nsqtZs
                  this.bd[index].nsqtZy = item.bmldzsBd.nsqtZy
                  this.bd[index].nsqtJwsj = item.bmldzsBd.nsqtJwsj
                  this.bd[index].nsqtQt = item.bmldzsBd.nsqtQt
                  this.bd[index].sum1 = item.bmldzsBd.bmqtZzbzr + item.bmldzsBd.bmqtZs + item.bmldzsBd.bmqtZy + item.bmldzsBd.bmqtJwsj + item.bmldzsBd.bmqtQt
                  this.bd[index].sum2 = item.bmldzsBd.nsqtZzbzr + item.bmldzsBd.nsqtZs + item.bmldzsBd.nsqtZy + item.bmldzsBd.nsqtJwsj + item.bmldzsBd.nsqtQt
                }
                this.bd[index].sum3 = item.cayjgxzSt + item.caewSt + item.caypSt + item.caltSt + item.caqtSt
                this.bd[index].sum4 = item.cazwslgSt + item.cazwfwjjgSt + item.cazwcbSt
              })
              this.dwbd = alldata.centre_dwbd
              this.dwbd.forEach((item, index) => {
                if (item.bmldzsBd) {
                  this.dwbd[index].bmldbz = item.bmldzsBd.bmldzzbz + item.bmldzsBd.bmldfzbz
                  this.dwbd[index].nsldbz = item.bmldzsBd.nsldzzbz + item.bmldzsBd.nsldfzbz
                  this.dwbd[index].bmqtZzbzr = item.bmldzsBd.bmqtZzbzr
                  this.dwbd[index].bmqtZs = item.bmldzsBd.bmqtZs
                  this.dwbd[index].bmqtZy = item.bmldzsBd.bmqtZy
                  this.dwbd[index].bmqtJwsj = item.bmldzsBd.bmqtJwsj
                  this.dwbd[index].bmqtQt = item.bmldzsBd.bmqtQt
                  this.dwbd[index].nsqtZzbzr = item.bmldzsBd.nsqtZzbzr
                  this.dwbd[index].nsqtZs = item.bmldzsBd.nsqtZs
                  this.dwbd[index].nsqtZy = item.bmldzsBd.nsqtZy
                  this.dwbd[index].nsqtJwsj = item.bmldzsBd.nsqtJwsj
                  this.dwbd[index].nsqtQt = item.bmldzsBd.nsqtQt
                  this.dwbd[index].sum1 = item.bmldzsBd.bmqtZzbzr + item.bmldzsBd.bmqtZs + item.bmldzsBd.bmqtZy + item.bmldzsBd.bmqtJwsj + item.bmldzsBd.bmqtQt
                  this.dwbd[index].sum2 = item.bmldzsBd.nsqtZzbzr + item.bmldzsBd.nsqtZs + item.bmldzsBd.nsqtZy + item.bmldzsBd.nsqtJwsj + item.bmldzsBd.nsqtQt
                }
                this.dwbd[index].sum3 = item.cayjgxzSt + item.caewSt + item.caypSt + item.caltSt + item.caqtSt
                this.dwbd[index].sum4 = item.cazwslgSt + item.cazwfwjjgSt + item.cazwcbSt
              })
            } else if (sourceTable === 'centre_sy') {
              this.unitInfo = (alldata.centre_sy && alldata.centre_sy.length > 0) ? alldata.centre_sy[0] : {}
              this.bd = alldata.centre_sy_bd
              this.bd.forEach((item, index) => {
                if (item.bmldzsBd) {
                  this.bd[index].bmldbz = item.bmldzsBd.bmldzzbz + item.bmldzsBd.bmldfzbz
                  this.bd[index].nsldbz = item.bmldzsBd.nsldzzbz + item.bmldzsBd.nsldfzbz
                  this.bd[index].bmqtZzbzr = item.bmldzsBd.bmqtZzbzr
                  this.bd[index].bmqtZs = item.bmldzsBd.bmqtZs
                  this.bd[index].bmqtZy = item.bmldzsBd.bmqtZy
                  this.bd[index].bmqtJwsj = item.bmldzsBd.bmqtJwsj
                  this.bd[index].bmqtQt = item.bmldzsBd.bmqtQt
                  this.bd[index].nsqtZzbzr = item.bmldzsBd.nsqtZzbzr
                  this.bd[index].nsqtZs = item.bmldzsBd.nsqtZs
                  this.bd[index].nsqtZy = item.bmldzsBd.nsqtZy
                  this.bd[index].nsqtJwsj = item.bmldzsBd.nsqtJwsj
                  this.bd[index].nsqtQt = item.bmldzsBd.nsqtQt
                  this.bd[index].sum1 = item.bmldzsBd.bmqtZzbzr + item.bmldzsBd.bmqtZs + item.bmldzsBd.bmqtZy + item.bmldzsBd.bmqtJwsj + item.bmldzsBd.bmqtQt
                  this.bd[index].sum2 = item.bmldzsBd.nsqtZzbzr + item.bmldzsBd.nsqtZs + item.bmldzsBd.nsqtZy + item.bmldzsBd.nsqtJwsj + item.bmldzsBd.nsqtQt
                }
                this.bd[index].sum0 = item.czbkbzrs + item.jfzlbzrs
                this.bd[index].sum3 = item.cayjgxzSt + item.caewSt + item.caypSt + item.caltSt + item.caqtSt
                this.bd[index].sum4 = item.cazwslgSt + item.cazwfwjjgSt + item.cazwcbSt
              })
              this.dwbd = alldata.centre_sy_dwbd
              this.dwbd.forEach((item, index) => {
                if (item.bmldzsBd) {
                  this.dwbd[index].bmldbz = item.bmldzsBd.bmldzzbz + item.bmldzsBd.bmldfzbz
                  this.dwbd[index].nsldbz = item.bmldzsBd.nsldzzbz + item.bmldzsBd.nsldfzbz
                  this.dwbd[index].bmqtZzbzr = item.bmldzsBd.bmqtZzbzr
                  this.dwbd[index].bmqtZs = item.bmldzsBd.bmqtZs
                  this.dwbd[index].bmqtZy = item.bmldzsBd.bmqtZy
                  this.dwbd[index].bmqtJwsj = item.bmldzsBd.bmqtJwsj
                  this.dwbd[index].bmqtQt = item.bmldzsBd.bmqtQt
                  this.dwbd[index].nsqtZzbzr = item.bmldzsBd.nsqtZzbzr
                  this.dwbd[index].nsqtZs = item.bmldzsBd.nsqtZs
                  this.dwbd[index].nsqtZy = item.bmldzsBd.nsqtZy
                  this.dwbd[index].nsqtJwsj = item.bmldzsBd.nsqtJwsj
                  this.dwbd[index].nsqtQt = item.bmldzsBd.nsqtQt
                  this.dwbd[index].sum0 = item.czbkbzrs + item.jfzlbzrs
                  this.dwbd[index].sum1 = item.bmldzsBd.bmqtZzbzr + item.bmldzsBd.bmqtZs + item.bmldzsBd.bmqtZy + item.bmldzsBd.bmqtJwsj + item.bmldzsBd.bmqtQt
                  this.dwbd[index].sum2 = item.bmldzsBd.nsqtZzbzr + item.bmldzsBd.nsqtZs + item.bmldzsBd.nsqtZy + item.bmldzsBd.nsqtJwsj + item.bmldzsBd.nsqtQt
                }
                this.dwbd[index].sum3 = item.cayjgxzSt + item.caewSt + item.caypSt + item.caltSt + item.caqtSt
                this.dwbd[index].sum4 = item.cazwslgSt + item.cazwfwjjgSt + item.cazwcbSt
              })
            } else if (sourceTable === 'depart') {
              this.unitInfo = (alldata.depart && alldata.depart.length > 0) ? alldata.depart[0] : {}
              this.dwbd = alldata.centre_dwbd
              if (this.dwbd) {
                this.dwbd.forEach((item, index) => {
                  if (item.bmldzsBd) {
                    this.dwbd[index].bmldbz = item.bmldzsBd.bmldzzbz + item.bmldzsBd.bmldfzbz
                    this.dwbd[index].nsldbz = item.bmldzsBd.nsldzzbz + item.bmldzsBd.nsldfzbz
                    this.dwbd[index].bmqtZzbzr = item.bmldzsBd.bmqtZzbzr
                    this.dwbd[index].bmqtZs = item.bmldzsBd.bmqtZs
                    this.dwbd[index].bmqtZy = item.bmldzsBd.bmqtZy
                    this.dwbd[index].bmqtJwsj = item.bmldzsBd.bmqtJwsj
                    this.dwbd[index].bmqtQt = item.bmldzsBd.bmqtQt
                    this.dwbd[index].nsqtZzbzr = item.bmldzsBd.nsqtZzbzr
                    this.dwbd[index].nsqtZs = item.bmldzsBd.nsqtZs
                    this.dwbd[index].nsqtZy = item.bmldzsBd.nsqtZy
                    this.dwbd[index].nsqtJwsj = item.bmldzsBd.nsqtJwsj
                    this.dwbd[index].nsqtQt = item.bmldzsBd.nsqtQt
                    this.dwbd[index].sum1 = item.bmldzsBd.bmqtZzbzr + item.bmldzsBd.bmqtZs + item.bmldzsBd.bmqtZy + item.bmldzsBd.bmqtJwsj + item.bmldzsBd.bmqtQt
                    this.dwbd[index].sum2 = item.bmldzsBd.nsqtZzbzr + item.bmldzsBd.nsqtZs + item.bmldzsBd.nsqtZy + item.bmldzsBd.nsqtJwsj + item.bmldzsBd.nsqtQt
                  }
                  this.dwbd[index].sum3 = item.cayjgxzSt + item.caewSt + item.caypSt + item.caltSt + item.caqtSt
                  this.dwbd[index].sum4 = item.cazwslgSt + item.cazwfwjjgSt + item.cazwcbSt
                })
              }
            }
            this.lxfs = (alldata.lxfs && alldata.lxfs.length > 0) ? alldata.lxfs[0] : {}
            this.loadingText = ''
          } else {
            console.log(response.data.message)
          }
        })
        .catch(error => {
          console.log(error)
          this.loadingText = '网络获取失败'
        })
    },
    // 保存
    saveData () {
      const lxfsPara = {
        lxfsTxdz: this.lxfs.lxfsTxdz,
        lxfsYzbm: this.lxfs.lxfsYzbm,
        lxfsLxr: this.lxfs.lxfsLxr,
        lxfsLxdh: this.lxfs.lxfsLxdh,
        lxfsZwym1: this.lxfs.lxfsZwym1,
        lxfsZwym2: this.lxfs.lxfsZwym2,
        lxfsYwym1: this.lxfs.lxfsYwym1,
        lxfsYwym2: this.lxfs.lxfsYwym2,
        lxfsBz: this.lxfs.lxfsBz
      }
      const commonPara = {
        unifyCode: this.unifyCode,
        bzsycj: this.bzsycj
      }
      if (this.sourceTable === 'office') {
        this.saveOffice()
      } else if (this.sourceTable === 'centre_sy_nsjg') {
        this.saveCentreSyNsjg()
      } else {
        if (this.infoTableName === 'first') {
          this.$refs.unitInfoForm1.validate((valid) => {
            if (valid) {
              this.saveDepart()
            }
          })
        } else if (this.infoTableName === 'second') {
          this.$refs.unitInfoForm2.validate((valid) => {
            if (valid) {
              if (!this.unifyCode) {
                this.$message({
                  message: '统一社会信用代码不能为空',
                  type: 'error'
                })
                return
              }
              if (!this.bzsycj) {
                this.$message({
                  message: '编制使用层次不能为空',
                  type: 'error'
                })
                return
              }
              this.$refs.lxfsForm1.validate((valid) => {
                if (valid) {
                  const regex = /^\d{6}$/
                  if (!regex.test(lxfsPara.lxfsYzbm)) {
                    this.$message({
                      message: '邮政编码长度为6位，且只能是数字',
                      type: 'error'
                    })
                    return
                  }
                  this.saveDepart(commonPara, lxfsPara)
                }
              })
            }
          })
        } else if (this.infoTableName === 'third') {
          this.$refs.unitInfoForm3.validate((valid) => {
            if (valid) {
              if (!this.unifyCode) {
                this.$message({
                  message: '统一社会信用代码不能为空',
                  type: 'error'
                })
                return
              }
              if (!this.bzsycj) {
                this.$message({
                  message: '编制使用层次不能为空',
                  type: 'error'
                })
                return
              }
              this.$refs.lxfsForm2.validate((valid) => {
                if (valid) {
                  const regex = /^\d{6}$/
                  if (!regex.test(lxfsPara.lxfsYzbm)) {
                    this.$message({
                      message: '邮政编码长度为6位，且只能是数字',
                      type: 'error'
                    })
                    return
                  }
                  this.saveCentreSy(commonPara, lxfsPara)
                }
              })
            }
          })
        }
      }
    },
    saveOffice () {
      const para = {
        jgsyCode: this.jgsyCode,
        oname2: this.officeInfo.oname2,
        ograde: this.officeInfo.ograde,
        odocNo: this.officeInfo.odocNo,
        oclass: this.officeInfo.oclass
      }
      this.btnLoading = true
      this.$http
        .post(this.$http.defaults.headcountAPI + 'api/office/save', para)
        .then(response => {
          if (response.data.success === true) {
            this.btnLoading = false
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          } else {
            console.log('保存失败:' + response.data.message)
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
            this.btnLoading = false
          }
        })
        .catch(error => {
          console.log(error)
          this.btnLoading = false
        })
    },
    saveCentreSyNsjg () {
      const para = {
        jgsyCode: this.jgsyCode,
        slnName2: this.centreSyNsjgInfo.slnName2,
        slnJggg: this.centreSyNsjgInfo.slnJggg,
        slnJgbzwh: this.centreSyNsjgInfo.slnJgbzwh,
        slnClass: this.centreSyNsjgInfo.slnClass
      }
      this.btnLoading = true
      this.$http
        .post(this.$http.defaults.headcountAPI + 'api/centreSyNsjg/save', para)
        .then(response => {
          if (response.data.success === true) {
            this.btnLoading = false
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          } else {
            console.log('保存失败:' + response.data.message)
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
            this.btnLoading = false
          }
        })
        .catch(error => {
          console.log(error)
          this.btnLoading = false
        })
    },
    saveDepart (commonPara, lxfsPara) {
      const para = {
        jgsyCode: this.jgsyCode,
        dname2: this.unitInfo.dname2,
        dgrade: this.unitInfo.dgrade,
        ddocNo: this.unitInfo.ddocNo,
        dclass: this.unitInfo.dclass
      }
      if (commonPara && commonPara.unifyCode && commonPara.bzsycj) {
        para.common = {...commonPara}
      }
      if (lxfsPara && lxfsPara.lxfsTxdz && lxfsPara.lxfsYzbm && lxfsPara.lxfsLxr && lxfsPara.lxfsLxdh) {
        para.lxfs = {...lxfsPara}
      }
      this.btnLoading = true
      this.$http
        .post(this.$http.defaults.headcountAPI + 'api/depart/save', para)
        .then(response => {
          if (response.data.success === true) {
            this.btnLoading = false
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          } else {
            console.log('保存失败:' + response.data.message)
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
            this.btnLoading = false
          }
        })
        .catch(error => {
          console.log(error)
          this.btnLoading = false
        })
    },
    saveCentreSy (commonPara, lxfsPara) {
      const para = {
        jgsyCode: this.jgsyCode,
        jgsx: this.unitInfo.jgsx,
        demc: this.unitInfo.demc,
        mcGfjc: this.unitInfo.mcGfjc,
        jggg: this.unitInfo.jggg,
        zgbm: this.unitInfo.zgbm,
        sydwlx: this.unitInfo.sydwlx,
        jfxs: this.unitInfo.jfxs,
        jnjw: this.unitInfo.jnjw,
        ifCg: this.unitInfo.ifCg,
        clsj: this.unitInfo.clsj,
        jgbzwh: this.unitInfo.jgbzwh,
        incorporator: this.unitInfo.incorporator
      }
      if (commonPara && commonPara.unifyCode && commonPara.bzsycj) {
        para.common = {...commonPara}
      }
      if (lxfsPara && lxfsPara.lxfsTxdz && lxfsPara.lxfsYzbm && lxfsPara.lxfsLxr && lxfsPara.lxfsLxdh) {
        para.lxfs = {...lxfsPara}
      }
      this.btnLoading = true
      this.$http
        .post(this.$http.defaults.headcountAPI + 'api/centreSy/save', para)
        .then(response => {
          if (response.data.success === true) {
            this.btnLoading = false
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          } else {
            console.log('保存失败:' + response.data.message)
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
            this.btnLoading = false
          }
        })
        .catch(error => {
          console.log(error)
          this.btnLoading = false
        })
    },
    // 格式化日期
    fclsj (date) {
      var mydate = new Date(date)
      var year = mydate.getFullYear()
      var month = mydate.getMonth() + 1
      var d = mydate.getDate()
      return year + '.' + month + '.' + d
    },
    setBgColor (id) {
      switch (id.toString()) {
        case '1':
          return 'background: #eee;'
        case '2':
          return 'background: #DFF1FF;'
        case '2-1':
          return 'background: #FFE8E8;'
        case '2-2':
          return 'background: #EBF3E4;'
        case '2-3':
          return 'background: #EBF2FA;'
        case '3':
          return 'background: #FDF7EC;'
        case '3-1':
          return 'background: #FFFFD7;'
        case '3-2':
          return 'background: #FFFFD7;'
        case '4':
          return 'background: #E1F7FF;'
        default:
          return ''
      }
    },
    changeShow (id) {
      id = id.toString()
      if (this.expandKeys.indexOf(id) > -1) {
        this.expandKeys.splice(this.expandKeys.indexOf(id), 1)
      } else {
        this.expandKeys.push(id)
      }
    }
  }
}
</script>

<style>
#info{
  overflow: hidden;
}

.my_table_title{
  font-weight: bolder;
}
/* .tableh th{
  width: 200px
}
.tableh td{
  text-align: left;
  padding-left:40px !important;
} */
</style>
